<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="规则名称" required prop="name">
            <el-input v-model="formItem.name" placeholder="请输入规则名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维护类别" required>
            <treeselect
              v-model="formItem.modelCode"
              :multiple="false"
              :options="cmdbModels"
              :load-options="loadOptions"
              :disable-branch-nodes="true"
              id-key="code"
              label-key="name"
              placeholder="请选择维护类别"
              @select="selModel"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="是否停止服务" prop="suspendService">
            <el-radio v-model="suspendService" :label="true" @change="changeSuspendService">是</el-radio>
            <el-radio v-model="suspendService" :label="false" @change="changeSuspendService">否</el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="量化时间(小时)" prop="quantifyingTime">
            <el-input-number v-model="formItem.quantifyingTime" :precision="0" :step="1" :max="10000" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="维护说明" prop="description">
            <Tinymce ref="tinymce" v-model="formItem.description" :height="300" menubar="false" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcom-ui/api/maintainRule'
import Tinymce from '@neu/biz-core/components/Tinymce'
import { pureTree } from '@neu/dcom-ui/api/schema'

const formItem = {
  name: '',
  modelCode: null,
  modelName: '',
  suspendService: '',
  quantifyingTime: null,
  description: '',
  source: 'newcreate'
}
export default {
  name: 'MaintainRuleEditForm',
  components: {
    Tinymce
  },
  props: {
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** CMDB模型树 */
      cmdbModels: null,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '规则名称不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 100) {
              callback(new Error('最多可输入100个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '维护类别不能为空',
          trigger: 'blur'
        }]
      },
      modal: true,
      /** 是否需要停止服务 */
      suspendService: true
    }
  },
  watch: {
    id: function() {
      this.fetch()
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    loadOptions({ action }) {
      if (action === 'LOAD_ROOT_OPTIONS') {
        return pureTree().then(response => {
          this.cmdbModels = response.data
        })
      }
    },
    selModel(node) {
      this.formItem.modelName = node.name
    },
    changeSuspendService(value) {
      this.formItem.suspendService = value
    }
  }
}
</script>
